package com.lanjiyin.cloud.common.util;

import java.util.Random;

/**
 * 随机数工具类
 * @author tudaojun
 *
 */
public class RandomUtil {
	/**
	 * 生成指定长度数字和字符
	 * @param length
	 * @return      
	 * String
	 */
	public static String getRandom(int length) {
		String val = "";
		Random random = new Random();
		for (int i = 0; i < length; i++) {
			// 输出字母还是数字
			String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
			// 字符串
			if ("char".equalsIgnoreCase(charOrNum)) {
				// 取得大写字母还是小写字母
				int choice = random.nextInt(2) % 2 == 0 ? 65 : 97;
				val += (char) (choice + random.nextInt(26));
			} else if ("num".equalsIgnoreCase(charOrNum)) { // 数字
				val += String.valueOf(random.nextInt(10));
			}
		}
		return val.toLowerCase();
	}
	/**
	 * 随机生成指定长度数字
	 * @param length
	 * @return      
	 * String
	 */
	public static String getRandomNumber(int length) {
		String val = "";
		Random random = new Random();
		for (int i = 0; i < length; i++) {
			val += String.valueOf(random.nextInt(10));
		}
		return val.toLowerCase();
	}
}
